package org.dromara.common.web.utils.jackson;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import org.dromara.common.web.utils.DateTimeUtil;

import java.io.IOException;
import java.util.Date;

public class DateDeserializer extends JsonDeserializer<Date> {

    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
            throws IOException {
        JsonNode node = jsonParser.getCodec().readTree(jsonParser);

        if (StrUtil.isNotEmpty(node.textValue())) {
            return DateTimeUtil.convertDate(node.textValue());
        }
        return null;
    }


    @Override
    public Date getNullValue(DeserializationContext ctxt) {
        return null;
    }
}
